package com.bwie.utils;

import com.alibaba.fastjson.JSON;
import com.bwie.mapper.TbDictionaryMapper;
import com.bwie.pojo.TbDictionary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/6/3 16:36
 */

@Component
public class DictionaryManager {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    TbDictionaryMapper tbDictionaryMapper;

    private final String DICTIONARY_KEY = "dictionary";

    /**
     * 向字典中添加一个条目。如果键已存在，则更新其值。
     */
    public void addEntry(String key, String value) {
        stringRedisTemplate.opsForHash().put(DICTIONARY_KEY, key, value);
    }

    /**
     * 从字典中移除指定的条目。
     */
    public void removeEntry(String key) {
        // 如果KEY存在，就删除
        if(stringRedisTemplate.opsForHash().hasKey(DICTIONARY_KEY, key)) {
            stringRedisTemplate.opsForHash().delete(DICTIONARY_KEY, key);
        }
    }

    /**
     * 根据给定的键返回对应的值。
     */
    public String getValue(String key) {
        if(stringRedisTemplate.opsForHash().hasKey(DICTIONARY_KEY, key)) {
            String s = (String) stringRedisTemplate.opsForHash().get(DICTIONARY_KEY, key);
            return s;
        }

        return null;
    }

    /**
     * 返回所有字典条目的列表。
     */
    public List<Map<String, String>> getAllEntries() {
        Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(DICTIONARY_KEY);

        ArrayList<Map<String, String>> list = new ArrayList<>();
        entries.keySet().forEach(key -> {
            HashMap<String, String> map = new HashMap<>();
            map.put(key.toString(), entries.get(key).toString());

            list.add(map);
        });

        return list;
    }

    /**
     * 清空所有的字典条目。
     */
    public void clearAllEntries() {
        stringRedisTemplate.delete(DICTIONARY_KEY);
    }

    /**
     * 将当前字典的状态保存到指定的文件路径中。
     */
    public void saveToDb() throws IOException {

        //-- 删除所有数据
        tbDictionaryMapper.delete(null);

        //-- 在写入数据
        List<Map<String, String>> allEntries = getAllEntries();
        for (Map<String, String> allEntry : allEntries) {
            Set<String> keySet = allEntry.keySet();
            String key = keySet.iterator().next();
            String value = allEntry.get(key);

            TbDictionary tbDictionary = new TbDictionary();
            tbDictionary.setDictName(key);
            tbDictionary.setDictValue(value);

            tbDictionaryMapper.insert(tbDictionary);
        }
    }

    /**
     * 从指定的文件路径中加载字典状态。
     */
    public void loadFromDb() throws IOException {
        List<TbDictionary> tbDictionaries = tbDictionaryMapper.selectList(null);
        for (TbDictionary tbDictionary : tbDictionaries) {
            addEntry(tbDictionary.getDictName(), tbDictionary.getDictValue());
        }
    }
}
